<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label=":">
        <el-select v-model="queryParams.storeId" placeholder="请选择" clearable size="small">
          <el-option v-for="option in options.storeIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
        </el-select>
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.storeName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-select v-model="queryParams.brandId" placeholder="请选择" clearable size="small">
          <el-option v-for="option in options.brandIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
        </el-select>
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.brandName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-select v-model="queryParams.supplierId" placeholder="请选择" clearable size="small">
          <el-option v-for="option in options.supplierIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
        </el-select>
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.supplierName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-select v-model="queryParams.mallId" placeholder="请选择" clearable size="small">
          <el-option v-for="option in options.mallIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
        </el-select>
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.mallName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:storeBrandRel:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:storeBrandRel:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:storeBrandRel:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="" align="center" prop="storeId">
        <template slot-scope="scope">
          <span>{{ storeIdFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="storeName" />
      <el-table-column label="" align="center" prop="brandId">
        <template slot-scope="scope">
          <span>{{ brandIdFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="brandName" />
      <el-table-column label="" align="center" prop="supplierId">
        <template slot-scope="scope">
          <span>{{ supplierIdFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="supplierName" />
      <el-table-column label="" align="center" prop="mallId">
        <template slot-scope="scope">
          <span>{{ mallIdFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="mallName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:storeBrandRel:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['basic:storeBrandRel:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="storeBrandRelFormDialog.title" :visible.sync="storeBrandRelFormDialog.visible" :close-on-click-modal="false" :before-close="resetStoreBrandRelFormDialog" width="500px" append-to-body>
      <el-form ref="storeBrandRelForm" :model="storeBrandRelFormData" :rules="storeBrandRelFormRules" label-width="80px">
        <el-form-item label="" prop="storeId">
          <el-select v-model="storeBrandRelFormData.storeId" style="width: 100%" clearable placeholder="请选择">
            <el-option v-for="option in options.storeIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="storeName">
          <el-input v-model="storeBrandRelFormData.storeName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="brandId">
          <el-select v-model="storeBrandRelFormData.brandId" style="width: 100%" clearable placeholder="请选择">
            <el-option v-for="option in options.brandIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="brandName">
          <el-input v-model="storeBrandRelFormData.brandName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="supplierId">
          <el-select v-model="storeBrandRelFormData.supplierId" style="width: 100%" clearable placeholder="请选择">
            <el-option v-for="option in options.supplierIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="supplierName">
          <el-input v-model="storeBrandRelFormData.supplierName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="mallId">
          <el-select v-model="storeBrandRelFormData.mallId" style="width: 100%" clearable placeholder="请选择">
            <el-option v-for="option in options.mallIdOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="mallName">
          <el-input v-model="storeBrandRelFormData.mallName" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmStoreBrandRelFormDialog">确 定</el-button>
        <el-button @click="resetStoreBrandRelFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import StoreBrandRelApi from '@/api/basic/StoreBrandRelApi'
import DictValueApi from '@/api/system/DictValueApi'

const defaultStoreBrandRelFormData = {
  storeId: null,
  storeName: null,
  brandId: null,
  brandName: null,
  supplierId: null,
  supplierName: null,
  mallId: null,
  mallName: null
}
export default {
  name: 'StoreBrandRelList',
  data() {
    return {
      options: {
        storeIdOptions: [], // 字典
        brandIdOptions: [], // 字典
        supplierIdOptions: [], // 字典
        mallIdOptions: [], // 字典
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        storeId: null,
        storeName: null,
        brandId: null,
        brandName: null,
        supplierId: null,
        supplierName: null,
        mallId: null,
        mallName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      storeBrandRelFormDialog: {
        title: '',
        visible: false
      },
      storeBrandRelFormData: Object.assign({}, defaultStoreBrandRelFormData),
      storeBrandRelFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      DictValueApi.list({ dictCode: 'oms_sales_order_status' }).then(response => {
        this.options.storeIdOptions = response.data
      })
      DictValueApi.list({ dictCode: 'sys_data_scope_type' }).then(response => {
        this.options.brandIdOptions = response.data
      })
      DictValueApi.list({ dictCode: 'oms_sales_order_source' }).then(response => {
        this.options.supplierIdOptions = response.data
      })
      DictValueApi.list({ dictCode: 'sys_data_scope_type' }).then(response => {
        this.options.mallIdOptions = response.data
      })
    },
    handleAdd() {
      this.openStoreBrandRelFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return StoreBrandRelApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openStoreBrandRelFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      StoreBrandRelApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return StoreBrandRelApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openStoreBrandRelFormDialog(id) {
      this.storeBrandRelFormDialog.visible = true
      if (id) {
        this.storeBrandRelFormDialog.title = 'storeBrand-修改'
        StoreBrandRelApi.get(id).then(response => {
          this.storeBrandRelFormData = {
            ...this.storeBrandRelFormData,
            ...response.data
          }
        })
      } else {
        this.storeBrandRelFormDialog.title = 'storeBrand-添加'
      }
    },
    confirmStoreBrandRelFormDialog() {
      this.$refs['storeBrandRelForm'].validate(valid => {
        if (valid) {
          if (this.storeBrandRelFormData.id) {
            StoreBrandRelApi.edit(this.storeBrandRelFormData).then(response => {
              this.$message.success('修改成功')
              this.resetStoreBrandRelFormDialog()
              this.handleQuery()
            })
          } else {
            StoreBrandRelApi.add(this.storeBrandRelFormData).then(response => {
              this.$message.success('添加成功')
              this.resetStoreBrandRelFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetStoreBrandRelFormDialog() {
      this.storeBrandRelFormDialog = {
        title: '',
        visible: false
      }
      this.storeBrandRelFormData = Object.assign({}, defaultStoreBrandRelFormData)
      this.resetForm('storeBrandRelForm')
    },
    // 字典
    storeIdFormat(row) {
      return this.$common.getDictValueName(this.options.storeIdOptions, row.storeId)
    },
    // 字典
    brandIdFormat(row) {
      return this.$common.getDictValueName(this.options.brandIdOptions, row.brandId)
    },
    // 字典
    supplierIdFormat(row) {
      return this.$common.getDictValueName(this.options.supplierIdOptions, row.supplierId)
    },
    // 字典
    mallIdFormat(row) {
      return this.$common.getDictValueName(this.options.mallIdOptions, row.mallId)
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
